package ext

import java.text.SimpleDateFormat
import java.util.*

private const val BAR_CODE_SIZE = 14

fun Long.toDateStr(): String {
    val date = Date(this)
    val format = SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
    return format.format(date)
}

// yyMMddHH
fun String.toTimeStamp(): Long {
    val dateStr = this.substring(8, BAR_CODE_SIZE)
    val month = dateStr.substring(0, 2).toInt()
    val date = dateStr.substring(2, 4).toInt()
    val hour = dateStr.substring(4, 6).toInt()

    val calendar = Calendar.getInstance()

    val nowMonth = calendar.get(Calendar.MONTH)
    if ((nowMonth == Calendar.JANUARY) && (month == 12)) {
        val year = calendar.get(Calendar.YEAR)
        calendar.set(Calendar.YEAR, year - 1)
    }

    calendar.set(Calendar.MONTH, month - 1)
    calendar.set(Calendar.DAY_OF_MONTH, date)
    calendar.set(Calendar.HOUR_OF_DAY, hour)
    calendar.set(Calendar.MINUTE, 0)
    calendar.set(Calendar.SECOND, 0)

    return calendar.timeInMillis
}




